package aau.client.geo;

import aau.client.ui.Actor;
import aau.client.ui.Circle;

import com.google.gwt.canvas.dom.client.Context2d;

public class StickyLine extends Actor {
    private Circle destination;
    private Circle from;

    public Circle getDestination() {
        return this.destination;
    }

    public Circle getFrom() {
        return this.from;
    }

    @Override
    public void paint(Context2d c) {
        Point l1 = this.from.getLocation();
        Point l2 = this.destination.getLocation();

        Vector line = Vector.createWithPoints(this.from.getLocation(), this.destination.getLocation());
        Point ct = l1.add(line.multiply(0.5d));

        Vector d1 = line.rotate(Math.PI / 2d).setScala(this.from.getRadius());
        Point c1 = l1.add(d1);
        Point c2 = l2.add(d1.setScala(this.destination.getRadius()));

        Vector d2 = line.rotate(-Math.PI / 2d).setScala(this.destination.getRadius());
        Point c3 = l2.add(d2);
        Point c4 = l1.add(d2.setScala(this.from.getRadius()));

        c.beginPath();
        c.moveTo(c1.x, c1.y);
        c.bezierCurveTo(ct.x, ct.y, l2.x, l2.y, c2.x, c2.y);

        c.lineTo(c3.x, c3.y);
        c.bezierCurveTo(l2.x, l2.y, ct.x, ct.y, c4.x, c4.y);

        c.closePath();
        c.setFillStyle("red");
        c.fill();

    }

    public void setDestination(Circle destination) {
        this.destination = destination;
    }

    public void setFrom(Circle from) {
        this.from = from;
    }
}
